package com.epoint.bookmanagement.action.bookinfo;

import java.awt.print.Book;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.crypto.spec.IvParameterSpec;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoint.bookmanagement.bizlogic.bookinfo.BookInfoService;
import com.epoint.bookmanagement.bizlogic.bookinfo.domain.BookInfo;
import com.epoint.bookmanagement.utils.RequestUtils;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.XXTEA;

/**
 * Servlet implementation class BookInfoListAction
 */
@WebServlet("/bookinfolistaction")
public class BookInfoListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookInfoListAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String requestMethod = request.getParameter("method");
		String json = RequestUtils.reflectInvokMethod(requestMethod, request,"com.epoint.bookmanagement.action.bookinfo.BookInfoListAction");
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	
	
	
	
	BookInfoService bookInfoService = new BookInfoService();
	/**
	 * 根据书名图书类型模糊查询图书信息
	 * @param request 请求
	 * @return 解雇
	 */
	public String queryBookinfoByKeyWords(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String pageSize = request.getParameter("pageSize");
		String bookName = request.getParameter("bookName");
		String bookType = request.getParameter("bookType");
		
		// 在已知分页后的数据集合
		List<BookInfo> list = bookInfoService.queryBookinfoByKeyWords(Integer.parseInt(pageIndex), Integer.parseInt(pageSize), 
				bookName, StrUtil.isEmpty(bookType) ? null : (Integer.parseInt(bookType)));
		
		// 重新计算总条数（在所有数据中进行模糊查询后的总条数）
		int total = bookInfoService.queryBookinfoByKeyWords(null, null, bookName, 
				StrUtil.isEmpty(bookType) ? null : (Integer.parseInt(bookType))).size();
		
		Map<String, Object> result = new HashMap<>(2);
		result.put("total", total);
		result.put("data", list);
		return JSONObject.toJSONString(result);
	}
	
	/**
	 * 根据id查询图书信息
	 * @param request 请求
	 * @return 结果
	 */
	public String queryBookInfoByID(HttpServletRequest request) {
		String bookId = request.getParameter("bookId");
		BookInfo result =  bookInfoService.queryBookInfoByID(bookId);
		return JSONObject.toJSONString(result);
	}
	
	/**
	 * 新增图书信息
	 * @param request 请求
	 * @return 结果
	 */
	public String addBookInfo(HttpServletRequest request) {
		String data = request.getParameter("data");
		BookInfo bookInfo = JSON.parseObject(data, BookInfo.class);
		// 重名验证
		BookInfo selectInfo = new BookInfo();
		selectInfo.setBookName(bookInfo.getBookName());
		selectInfo.setAuthor(bookInfo.getAuthor());
		System.out.println(selectInfo);
		List<BookInfo> list = bookInfoService.queryBookinfoByBookInfo(null, null, selectInfo, null);
		if (list.size() > 0) {
			return "该作者下已存在这本图书，请重新添加！";
		}
		System.out.println("ok");
		bookInfo.setBookId(UUID.randomUUID().toString());
		int i = bookInfoService.addBookInfo(bookInfo);
		if (i > 0) {
			return "添加成功";
		} else {
			return "添加失败/(ㄒoㄒ)/~~";
		}
	}
	
	
	/**
	 * 更新操作
	 * @param bookinfo 书信息
	 * @return 条数
	 */
	public String updateBookinfo(HttpServletRequest request) {
		String data = request.getParameter("data");
		BookInfo bookInfo = JSON.parseObject(data, BookInfo.class);
		String beforeBookId = bookInfo.getBookId();
		String nowBookName = bookInfo.getBookName();
		String author = bookInfo.getAuthor();
		// 重名验证
		BookInfo exist = bookInfoService.checkExist(beforeBookId, nowBookName, author);
		if (ObjectUtil.isNotNull(exist)) {
			return "该作者下已存在这本图书，请重新修改！";
		}
		
		int i = bookInfoService.updateBookinfo(bookInfo);
		if (i > 0) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}
	
	/**
	 * 隔开的多个id，实现单表的多选删除
	 * @param request 请求
	 * @return 结果
	 */
	public String deleteBookinfo(HttpServletRequest request) {
		String bookIds = request.getParameter("bookIds");
		return bookInfoService.deleteBookinfo(bookIds);
	}
	
	
	
}
